/* inflows into the DEPARTEMENT */
/*************************************/
/* Inflows into the occupation group */
/*************************************/
capture program drop inflows
program define inflows
version 11
syntax, occp(name)

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\dep_unbalanced.dta", clear
replace cad2=(cs2h=="33" | cs2h=="34" | cs2h=="37" | cs2h=="38")

gen orig=(ptreat==1 & `occp'==1 )
/* gen inflow=(dtreat==1 & bc==1 & (L.bc!=1 | L.ize!=ize) & !missing(L.ize)) */
gen inflowr=(dtreat==1 & `occp'==1 & L.ize!=ize )

gen sond=12
replace sond=24 if an==2007

save temp1.dta, replace
collapse (sum) norig=orig ninflow=inflowr [fweight = sond], by(ze an)
save p1.dta, replace
use temp1.dta, clear
collapse dimm dimm75 Y_* , by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen inflr=(D.ninflow)/norig

drop if missing(dimm75)
/* OLS */
ivreg2 inflr dimm Y_*  , partial(Y_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 inflr (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'iv

keep ze an inflr
save tempX.dta, replace

use temp1.dta, clear
joinby ze an using tempX.dta, unm(m)
drop _merge

gen inflow=(`occp'==1 & L.ize==ize )
/*keep if dtreat==1*/
keep if `occp'==1
/************************************************************/
/* location in the decile of the initial group distribution */
/* fixed effects age x an */
xi i.age*i.an , pre(F_)
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd), by(ze an) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen inter75= dimm75 * L.qinit

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

ivreg2 inflr (dimm inter = dimm75 inter75) lqinit  Y_*  [aweight = iw2], partial(Y_*) cl(ze)
estimates store `occp'iv2

end

inflows, occp(all) 
inflows, occp(cad2) 
inflows, occp(tecemp2) 
inflows, occp(bc) 
inflows, occp(bcnq) 


capture rm "C:\Users\Public\Documents\Tab21\TableA12.rtf"
/* OLS */
esttab allols  cad2ols tecemp2ols bcols bcnqols ///
using "C:\Users\Public\Documents\Tab21\TableA12.rtf" /// 
, append title("OLS_CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cad2iv tecemp2iv bciv bcnqiv ///
using "C:\Users\Public\Documents\Tab21\TableA12.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

  esttab alliv2  cad2iv2 tecemp2iv2 bciv2 bcnqiv2 ///
using "C:\Users\Public\Documents\Tab21\TableA12.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
